 

package com.zdkj.subway.st.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.subway.st.entity.SwTkOfficialTicketHistory;
import com.zdkj.subway.st.service.SwTkOfficialTicketHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;


/**
 * 公务票办理历史信息
 *
 * @author chenbo
 * @date 2020-06-10 10:21:03
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swtkofficialtickethistory" )
@Api(value = "swtkofficialtickethistory", tags = "公务票办理历史信息管理")
public class SwTkOfficialTicketHistoryController {

    private final  SwTkOfficialTicketHistoryService swTkOfficialTicketHistoryService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param swTkOfficialTicketHistory 公务票办理历史信息
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getSwTkOfficialTicketHistoryPage(Page page, SwTkOfficialTicketHistory swTkOfficialTicketHistory) {
        String nameLike=null;
        String cardNumLike=null;
        String idNumLike=null;
        String oldCardNumLike = null;
        String applyNumLike = null;
        String underCenterLike = null;
        String belongUnitLike = null;
        //票卡状态 1：已过期 2：正常  虚拟标识
        String ticketStatus = swTkOfficialTicketHistory.getTicketStatus();

        if (swTkOfficialTicketHistory.getName()!=null){
            nameLike=swTkOfficialTicketHistory.getName();
            swTkOfficialTicketHistory.setName(null);
        }
        if (swTkOfficialTicketHistory.getCardNum()!=null){
            cardNumLike=swTkOfficialTicketHistory.getCardNum();
            swTkOfficialTicketHistory.setCardNum(null);
        }
        if (swTkOfficialTicketHistory.getIdCard()!=null){
            idNumLike=swTkOfficialTicketHistory.getIdCard();
            swTkOfficialTicketHistory.setIdCard(null);
        }
        if (swTkOfficialTicketHistory.getCardNum() != null) {
            oldCardNumLike = swTkOfficialTicketHistory.getOldCardNum();
            swTkOfficialTicketHistory.setOldCardNum(null);
        }
        if (swTkOfficialTicketHistory.getApplyNum() != null) {
            applyNumLike = swTkOfficialTicketHistory.getApplyNum();
            swTkOfficialTicketHistory.setApplyNum(null);
        }
        if (swTkOfficialTicketHistory.getUnderCenter() != null) {
            underCenterLike = swTkOfficialTicketHistory.getUnderCenter();
            swTkOfficialTicketHistory.setUnderCenter(null);
        }
        if (swTkOfficialTicketHistory.getBelongUnit() != null) {
            belongUnitLike = swTkOfficialTicketHistory.getBelongUnit();
            swTkOfficialTicketHistory.setBelongUnit(null);
        }
        if (swTkOfficialTicketHistory.getTicketStatus() != null) {
            swTkOfficialTicketHistory.setTicketStatus(null);
        }
        LambdaQueryWrapper<SwTkOfficialTicketHistory> queryWrapper = Wrappers.query(swTkOfficialTicketHistory).orderByDesc("send_card_time").lambda();
        queryWrapper.eq(SwTkOfficialTicketHistory::getOfficialType,"0");
        List<String> dateConditionfk = swTkOfficialTicketHistory.getDates1();
        if (ObjectUtil.isNotEmpty(dateConditionfk)) {
            queryWrapper.between(SwTkOfficialTicketHistory::getSendCardTime, dateConditionfk.get(0), dateConditionfk.get(1));
        }
        if(swTkOfficialTicketHistory.getEndTimeDTO()!= null){
            queryWrapper.likeRight(SwTkOfficialTicketHistory::getEndTime,swTkOfficialTicketHistory.getEndTimeDTO());
        }
        //用来区分票卡状态 1：已过期 2：正常
        //判断时间 如果当前时间>=票卡结束时间 代表已过期 否则代表正常
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        if("1".equals(ticketStatus)){
            queryWrapper.lt(SwTkOfficialTicketHistory::getEndTime,now);
        }
        if("2".equals(ticketStatus)){
            queryWrapper.ge(SwTkOfficialTicketHistory::getEndTime,now);
        }
        return R.ok(swTkOfficialTicketHistoryService.page(page, queryWrapper
        .like(nameLike==null?false:true,SwTkOfficialTicketHistory::getName,nameLike)
                .like(cardNumLike==null?false:true,SwTkOfficialTicketHistory::getCardNum,cardNumLike)
                .like(idNumLike==null?false:true,SwTkOfficialTicketHistory::getIdCard,idNumLike)
                .like(StringUtils.isNotEmpty(oldCardNumLike), SwTkOfficialTicketHistory::getOldCardNum, oldCardNumLike)
                .like(StringUtils.isNotEmpty(applyNumLike), SwTkOfficialTicketHistory::getApplyNum, applyNumLike)
                .like(StringUtils.isNotEmpty(underCenterLike), SwTkOfficialTicketHistory::getUnderCenter, underCenterLike)
                .like(StringUtils.isNotEmpty(belongUnitLike), SwTkOfficialTicketHistory::getBelongUnit, belongUnitLike)
                .eq(StringUtils.isNotEmpty(swTkOfficialTicketHistory.getBackStatus()), SwTkOfficialTicketHistory::getBackStatus,swTkOfficialTicketHistory.getBackStatus())));
    }
    /**
     * 临时通行分页查询
     * @param page 分页对象
     * @param swTkOfficialTicketHistory 公务票办理历史信息
     * @return
     */
    @ApiOperation(value = "临时通行分页查询", notes = "临时通行分页查询")
    @GetMapping("/pageTemporary" )
    public R getSwTkOfficialTicketHistoryTemporaryPage(Page page, SwTkOfficialTicketHistory swTkOfficialTicketHistory) {
        String nameLike=null;
        String cardNumLike=null;
        String idNumLike=null;
        String oldCardNumLike = null;
        String applyNumLike = null;
        String underCenterLike = null;
        String belongUnitLike = null;
        //票卡状态 1：已过期 2：正常  虚拟标识
        String ticketStatus = swTkOfficialTicketHistory.getTicketStatus();

        if (swTkOfficialTicketHistory.getName()!=null){
            nameLike=swTkOfficialTicketHistory.getName();
            swTkOfficialTicketHistory.setName(null);
        }
        if (swTkOfficialTicketHistory.getCardNum()!=null){
            cardNumLike=swTkOfficialTicketHistory.getCardNum();
            swTkOfficialTicketHistory.setCardNum(null);
        }
        if (swTkOfficialTicketHistory.getCardNum()!=null){
            idNumLike=swTkOfficialTicketHistory.getIdCard();
            swTkOfficialTicketHistory.setIdCard(null);
        }
        if (swTkOfficialTicketHistory.getCardNum() != null) {
            oldCardNumLike = swTkOfficialTicketHistory.getOldCardNum();
            swTkOfficialTicketHistory.setOldCardNum(null);
        }
        if (swTkOfficialTicketHistory.getApplyNum() != null) {
            applyNumLike = swTkOfficialTicketHistory.getApplyNum();
            swTkOfficialTicketHistory.setApplyNum(null);
        }
        if (swTkOfficialTicketHistory.getUnderCenter() != null) {
            underCenterLike = swTkOfficialTicketHistory.getUnderCenter();
            swTkOfficialTicketHistory.setUnderCenter(null);
        }
        if (swTkOfficialTicketHistory.getBelongUnit() != null) {
            belongUnitLike = swTkOfficialTicketHistory.getBelongUnit();
            swTkOfficialTicketHistory.setBelongUnit(null);
        }
        if (swTkOfficialTicketHistory.getTicketStatus() != null) {
            swTkOfficialTicketHistory.setTicketStatus(null);
        }
        LambdaQueryWrapper<SwTkOfficialTicketHistory> queryWrapper = Wrappers.query(swTkOfficialTicketHistory).lambda();
        queryWrapper.eq(SwTkOfficialTicketHistory::getOfficialType,"1");
        List<String> dateConditionfk = swTkOfficialTicketHistory.getDates1();
        if (ObjectUtil.isNotEmpty(dateConditionfk)) {
            queryWrapper.between(SwTkOfficialTicketHistory::getSendCardTime, dateConditionfk.get(0), dateConditionfk.get(1));
        }
        if(swTkOfficialTicketHistory.getEndTimeDTO()!= null){
            queryWrapper.likeRight(SwTkOfficialTicketHistory::getEndTime,swTkOfficialTicketHistory.getEndTimeDTO());
        }
        //用来区分票卡状态 1：已过期 2：正常
        //判断时间 如果当前时间>=票卡结束时间 代表已过期 否则代表正常
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        if("1".equals(ticketStatus)){
            queryWrapper.lt(SwTkOfficialTicketHistory::getEndTime,now);
        }
        if("2".equals(ticketStatus)){
            queryWrapper.ge(SwTkOfficialTicketHistory::getEndTime,now);
        }
        return R.ok(swTkOfficialTicketHistoryService.page(page, queryWrapper
                .like(nameLike==null?false:true,SwTkOfficialTicketHistory::getName,nameLike)
                .like(cardNumLike==null?false:true,SwTkOfficialTicketHistory::getCardNum,cardNumLike)
                .like(idNumLike==null?false:true,SwTkOfficialTicketHistory::getIdCard,idNumLike)
                .like(StringUtils.isNotEmpty(oldCardNumLike), SwTkOfficialTicketHistory::getOldCardNum, oldCardNumLike)
                .like(StringUtils.isNotEmpty(applyNumLike), SwTkOfficialTicketHistory::getApplyNum, applyNumLike)
                .like(StringUtils.isNotEmpty(underCenterLike), SwTkOfficialTicketHistory::getUnderCenter, underCenterLike)
                .like(StringUtils.isNotEmpty(belongUnitLike), SwTkOfficialTicketHistory::getBelongUnit, belongUnitLike)
                .eq(StringUtils.isNotEmpty(swTkOfficialTicketHistory.getBackStatus()), SwTkOfficialTicketHistory::getBackStatus,swTkOfficialTicketHistory.getBackStatus())));
    }


    /**
     * 员工卡分页查询
     * @param page 分页对象
     * @param swTkOfficialTicketHistory 公务票办理历史信息
     * @return
     */
    @ApiOperation(value = "员工卡分页查询", notes = "员工卡分页查询")
    @GetMapping("/pageStaffCard" )
    public R getSwTkOfficialTicketHistoryStaffCardPage(Page page, SwTkOfficialTicketHistory swTkOfficialTicketHistory) {
        String nameLike=null;
        String cardNumLike=null;
        String idNumLike=null;
        String oldCardNumLike = null;
        String applyNumLike = null;
        String underCenterLike = null;
        String belongUnitLike = null;
        //票卡状态 1：已过期 2：正常  虚拟标识
        String ticketStatus = swTkOfficialTicketHistory.getTicketStatus();

        if (swTkOfficialTicketHistory.getName()!=null){
            nameLike=swTkOfficialTicketHistory.getName();
            swTkOfficialTicketHistory.setName(null);
        }
        if (swTkOfficialTicketHistory.getCardNum()!=null){
            cardNumLike=swTkOfficialTicketHistory.getCardNum();
            swTkOfficialTicketHistory.setCardNum(null);
        }
        if (swTkOfficialTicketHistory.getCardNum()!=null){
            idNumLike=swTkOfficialTicketHistory.getIdCard();
            swTkOfficialTicketHistory.setIdCard(null);
        }
        if (swTkOfficialTicketHistory.getCardNum() != null) {
            oldCardNumLike = swTkOfficialTicketHistory.getOldCardNum();
            swTkOfficialTicketHistory.setOldCardNum(null);
        }
        if (swTkOfficialTicketHistory.getApplyNum() != null) {
            applyNumLike = swTkOfficialTicketHistory.getApplyNum();
            swTkOfficialTicketHistory.setApplyNum(null);
        }
        if (swTkOfficialTicketHistory.getUnderCenter() != null) {
            underCenterLike = swTkOfficialTicketHistory.getUnderCenter();
            swTkOfficialTicketHistory.setUnderCenter(null);
        }
        if (swTkOfficialTicketHistory.getBelongUnit() != null) {
            belongUnitLike = swTkOfficialTicketHistory.getBelongUnit();
            swTkOfficialTicketHistory.setBelongUnit(null);
        }
        if (swTkOfficialTicketHistory.getTicketStatus() != null) {
            swTkOfficialTicketHistory.setTicketStatus(null);
        }
        LambdaQueryWrapper<SwTkOfficialTicketHistory> queryWrapper = Wrappers.query(swTkOfficialTicketHistory).lambda();
        queryWrapper.eq(SwTkOfficialTicketHistory::getOfficialType,"3");
        List<String> dateConditionfk = swTkOfficialTicketHistory.getDates1();
        if (ObjectUtil.isNotEmpty(dateConditionfk)) {
            queryWrapper.between(SwTkOfficialTicketHistory::getSendCardTime, dateConditionfk.get(0), dateConditionfk.get(1));
        }
        if(swTkOfficialTicketHistory.getEndTimeDTO()!= null){
            queryWrapper.likeRight(SwTkOfficialTicketHistory::getEndTime,swTkOfficialTicketHistory.getEndTimeDTO());
        }
        //用来区分票卡状态 1：已过期 2：正常
        //判断时间 如果当前时间>=票卡结束时间 代表已过期 否则代表正常
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        if("1".equals(ticketStatus)){
            queryWrapper.lt(SwTkOfficialTicketHistory::getEndTime,now);
        }
        if("2".equals(ticketStatus)){
            queryWrapper.ge(SwTkOfficialTicketHistory::getEndTime,now);
        }
        return R.ok(swTkOfficialTicketHistoryService.page(page, queryWrapper
                .like(nameLike==null?false:true,SwTkOfficialTicketHistory::getName,nameLike)
                .like(cardNumLike==null?false:true,SwTkOfficialTicketHistory::getCardNum,cardNumLike)
                .like(idNumLike==null?false:true,SwTkOfficialTicketHistory::getIdCard,idNumLike)
                .like(StringUtils.isNotEmpty(oldCardNumLike), SwTkOfficialTicketHistory::getOldCardNum, oldCardNumLike)
                .like(StringUtils.isNotEmpty(applyNumLike), SwTkOfficialTicketHistory::getApplyNum, applyNumLike)
                .like(StringUtils.isNotEmpty(underCenterLike), SwTkOfficialTicketHistory::getUnderCenter, underCenterLike)
                .like(StringUtils.isNotEmpty(belongUnitLike), SwTkOfficialTicketHistory::getBelongUnit, belongUnitLike)
                .eq(StringUtils.isNotEmpty(swTkOfficialTicketHistory.getBackStatus()), SwTkOfficialTicketHistory::getBackStatus,swTkOfficialTicketHistory.getBackStatus())));
    }


    /**
     * 导出公务票历史信息本站卡
     *
     * @param swTkOfficialTicketHistory 公务票信息
     * @return
     */
    @ApiOperation(value = "公务票历史信息本站卡导出", notes = "公务票历史信息本站卡导出")
    @GetMapping("/export")
    //TODO 待确定状态字段
    @PreAuthorize("@pms.hasPermission('st_swtkofficialtickethistory_export')" )
    public void getSwStInvoiceBalanceExport(SwTkOfficialTicketHistory swTkOfficialTicketHistory, HttpServletResponse response) throws IOException {
        swTkOfficialTicketHistory.setOfficialType("0");//本站卡
        swTkOfficialTicketHistoryService.export(swTkOfficialTicketHistory,response);
    }

    /**
     * 导出公务票历史信息临时通行卡
     *
     * @param swTkOfficialTicketHistory 公务票信息
     * @return
     */
    @ApiOperation(value = "公务票历史信息临时通行卡导出", notes = "公务票历史信息临时通行卡导出")
    @GetMapping("/exportTemporary")
    //TODO 待确定状态字段
    @PreAuthorize("@pms.hasPermission('st_swtkofficialtickethistory_export')" )
    public void getSwStInvoiceBalanceTemporaryExport(SwTkOfficialTicketHistory swTkOfficialTicketHistory, HttpServletResponse response) throws IOException {
        swTkOfficialTicketHistory.setOfficialType("1");//临时通行卡
        swTkOfficialTicketHistoryService.export(swTkOfficialTicketHistory,response);
    }

    /**
     * 导出公务票历史信息员工卡
     * @param swTkOfficialTicketHistory 公务票信息
     * @return
     */
    @ApiOperation(value = "公务票历史信息员工卡导出", notes = "公务票历史信息员工卡导出")
    @GetMapping("/exportStaffCard")
    //TODO 待确定状态字段
    @PreAuthorize("@pms.hasPermission('st_swtkofficialtickethistory_export')" )
    public void getSwStInvoiceBalanceStaffCardExport(SwTkOfficialTicketHistory swTkOfficialTicketHistory, HttpServletResponse response) throws IOException {
        swTkOfficialTicketHistory.setOfficialType("3");//员工卡
        swTkOfficialTicketHistoryService.export(swTkOfficialTicketHistory,response);
    }
    /**
     * 通过id查询公务票办理历史信息
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(swTkOfficialTicketHistoryService.getById(id));
    }

    /**
     * 新增公务票办理历史信息
     * @param swTkOfficialTicketHistory 公务票办理历史信息
     * @return R
     */
    @ApiOperation(value = "新增公务票办理历史信息", notes = "新增公务票办理历史信息")
    @SysLog("新增公务票办理历史信息" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('st_swtkofficialtickethistory_add')" )
    public R save(@RequestBody SwTkOfficialTicketHistory swTkOfficialTicketHistory) {
        return R.ok(swTkOfficialTicketHistoryService.save(swTkOfficialTicketHistory));
    }

    /**
     * 修改公务票办理历史信息
     * @param swTkOfficialTicketHistory 公务票办理历史信息
     * @return R
     */
    @ApiOperation(value = "修改公务票办理历史信息", notes = "修改公务票办理历史信息")
    @SysLog("修改公务票办理历史信息" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('st_swtkofficialtickethistory_edit')" )
    public R updateById(@RequestBody SwTkOfficialTicketHistory swTkOfficialTicketHistory) {
        return R.ok(swTkOfficialTicketHistoryService.updateById(swTkOfficialTicketHistory));
    }

    /**
     * 通过id删除公务票办理历史信息
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除公务票办理历史信息", notes = "通过id删除公务票办理历史信息")
    @SysLog("通过id删除公务票办理历史信息" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('st_swtkofficialtickethistory_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(swTkOfficialTicketHistoryService.removeById(id));
    }

}
